
package com.viweb.storemanager.temp;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class StyleDocument extends PlainDocument {

    public static final byte STYLE_DEFAULT = 0;
    public static final byte STYLE_UPPER = 1;
    public static final byte STYLE_LOWER = 2;
    public static final byte STYLE_NUMBER = 3;
    private int maxLength = 0;
    private byte style;

    /**
     * @author Minh NGUYEN
     * Creates new StyleDocument object
     * @param limit limit of the input text length
     */
    public StyleDocument(int maxLength) {
        super();
        this.maxLength = maxLength;
    }

    /**
     * @author Minh NGUYEN
     * Creates new StyleDocument object
     * @param limit limit of the input text length
     * @param style style that the text component have to follow
     */
    public StyleDocument(int maxLength, byte style) {
        this(maxLength);
        this.setStyle(style);
    }

    /**
     * @author Minh NGUYEN
     * Creates new StyleDocument object
     * @param limit limit of the input text length
     * @param style style that the text component have to follow
     */
    public StyleDocument(byte style) {
        this.setStyle(style);
    }

    @Override
    public void insertString(int offset, String str, AttributeSet attr) throws
            BadLocationException {

        if (this.maxLength > 0) {
            if (str != null && ((getLength() + str.length()) <= maxLength)) {
                if (this.getStyle() == STYLE_NUMBER) {
                    int i = -1;
                    boolean isDigit = true;

                    while (++i < str.length()) {
                        if (!Character.isDigit(str.charAt(i))) {
                            isDigit = !isDigit;
                            return;
                        }
                    }
                    super.insertString(offset, str, attr);
                } else if (this.getStyle() == STYLE_UPPER) {
                    super.insertString(offset, str.toUpperCase(), attr);
                } else if (this.getStyle() == STYLE_LOWER) {
                    super.insertString(offset, str.toLowerCase(), attr);
                } else {
                    if (offset == 0) {
                        super.insertString(offset, str.toUpperCase(), attr);
                    } else {
                        super.insertString(offset, str, attr);
                    }
                }
            } else {
                java.awt.Toolkit.getDefaultToolkit().beep();
            }
        } else {
            if (this.getStyle() == STYLE_NUMBER) {
                int i = -1;
                boolean isDigit = true;

                while (++i < str.length()) {
                    if (!Character.isDigit(str.charAt(i))) {
                        isDigit = !isDigit;
                        return;
                    }
                }
                super.insertString(offset, str, attr);
            } else if (this.getStyle() == STYLE_UPPER) {
                super.insertString(offset, str.toUpperCase(), attr);
            } else if (this.getStyle() == STYLE_LOWER) {
                super.insertString(offset, str.toLowerCase(), attr);
            } else {
                if (offset == 0) {
                    super.insertString(offset, str.toUpperCase(), attr);
                } else {
                    super.insertString(offset, str, attr);
                }
            }
        }
    }

    public byte getStyle() {
        return style;
    }

    public void setStyle(byte style) {
        this.style = style;
    }
}
